#include "app_data.h"

////////////////////////////////////////////////////////////////
// 接收消息
void rxdata(const uint8_t *mac, const uint8_t *incomingData, int len)
{
      if (mac[0] == USBpower_Address[0] and mac[1] == USBpower_Address[1] and mac[2] == USBpower_Address[2] and mac[3] == USBpower_Address[3] and mac[4] == USBpower_Address[4] and mac[5] == USBpower_Address[5])
      {
            memcpy(&USB_power_data, incomingData, sizeof(USB_power_data));
            USBdata_refresh.USB_OUT1 = USB_power_data.usbdata1;
            USBdata_refresh.USB_OUT2 = USB_power_data.usbdata2;
            USBdata_refresh.USB_OUT3 = USB_power_data.usbdata3;
            USBdata_refresh.USB_OUT4 = USB_power_data.usbdata4;
      }
      else if (mac[0] == SmartLamp_Address[0] and mac[1] == SmartLamp_Address[1] and mac[2] == SmartLamp_Address[2] and mac[3] == SmartLamp_Address[3] and mac[4] == SmartLamp_Address[4] and mac[5] == SmartLamp_Address[5])
      {
            memcpy(&SmartLamp_data, incomingData, sizeof(SmartLamp_data));
            switch (SmartLamp_data.lampdata1)
            {
            case LAMP_CHANGE_COLORT_MODE: // 调色温模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.delay_time = SmartLamp_data.lampdata4;
                  lampdata_refresh.setting = SmartLamp_data.lampdata5;
                  break;
            case LAMP_COMPUTER_MODE: // 电脑模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.delay_time = SmartLamp_data.lampdata4;
                  lampdata_refresh.setting = SmartLamp_data.lampdata5;
                  break;
            case LAMP_READ_MODE: // 阅读模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.delay_time = SmartLamp_data.lampdata4;
                  lampdata_refresh.setting = SmartLamp_data.lampdata5;
                  break;
            case LAMP_COZY_HOME_MODE: // 温馨模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.delay_time = SmartLamp_data.lampdata4;
                  lampdata_refresh.setting = SmartLamp_data.lampdata5;
                  break;
            case LAMP_CINEMA_MODE: // 电影院模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.delay_time = SmartLamp_data.lampdata4;
                  lampdata_refresh.setting = SmartLamp_data.lampdata5;
                  break;
            case LAMP_MUSIC_MODE: // 音乐模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.volume = SmartLamp_data.lampdata4;
                  lampdata_refresh.rgb_mode = SmartLamp_data.lampdata5;
                  break;
            case LAMP_RGB_MODE: // 彩灯模式
                  lampdata_refresh.lamp_mode = SmartLamp_data.lampdata1;
                  lampdata_refresh.brightness = SmartLamp_data.lampdata2;
                  lampdata_refresh.colorT = SmartLamp_data.lampdata3;
                  lampdata_refresh.lamp_hsv = SmartLamp_data.lampdata4;
                  lampdata_refresh.rgb_mode = SmartLamp_data.lampdata5;
                  break;
            }
      }
}
// 创建一个发射回调函数作为发射数据后的串口显示
// 发送时的回调函数
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
      char macStr[18];
      Serial.print("Packet to: ");
      /**串口提示向哪块板子发送**/
      snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
               mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
      /** 以下几句将在串口输出接收端的板子是否接收到了消息，方便调试	**/
      Serial.print(macStr);
      Serial.print(" send status:\t");
      Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void espnowbegin()
{
      WiFi.mode(WIFI_STA);
      if (esp_now_init() != ESP_OK)
      {
            Serial.println("ErrorESPNOW");
            return;
      }

      esp_now_register_send_cb(OnDataSent);

      esp_now_register_recv_cb(rxdata);

      esp_now_peer_info_t peerInfo;
      peerInfo.ifidx = WIFI_IF_STA;
      peerInfo.channel = 0;
      peerInfo.encrypt = false;
      // 配置第一块接收接收的Mac地址
      memcpy(peerInfo.peer_addr, USBpower_Address, 6);
      if (esp_now_add_peer(&peerInfo) != ESP_OK)
      {
            Serial.println("Failed to add peer");
            return;
      }
      // 配置第二块接收接收的Mac地址
      memcpy(peerInfo.peer_addr, SmartLamp_Address, 6);
      if (esp_now_add_peer(&peerInfo) != ESP_OK)
      {
            Serial.println("Failed to add peer");
            return;
      }

      // if (esp_now_add_peer(&peerInfo) != ESP_OK)
      // {
      //     Serial.println("Failed to add peer");
      //     return;
      // }
}